package com.org.khtn.gui;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class MyTableDesign extends AbstractTableModel {
	private JTable table;
	private Vector colHeaders;
	private Vector tbData;
	public boolean isModify;

	public MyTableDesign(JTable tb) {
		table = tb;
		isModify = false;
		String[] s = new String[] { "", "Column Name", "Data Type" };

		colHeaders = new Vector(s.length);
		tbData = new Vector();
		for (int i = 0; i < s.length; i++) {
			colHeaders.addElement(s[i]);
		}
	}

	public int getColumnCount() {
		return colHeaders.size();
	}

	public int getRowCount() {
		return tbData.size();
	}

	public String getColumnName(int col) {
		return colHeaders.get(col).toString();
	}

	public Object getValueAt(int row, int column) {
		Vector rowData = (Vector) (tbData.elementAt(row));
		return rowData.elementAt(column);
	}

	public void setValueAt(Object value, int row, int col) {
		Vector v = (Vector) tbData.get(row);
		v.set(col, value);
		fireTableCellUpdated(row, col);
	}

	public void insertRow(Vector v) {
		tbData.addElement(v);
		fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
	}

	public void deleteRow(int index) {
		tbData.removeElementAt(index);
		fireTableRowsDeleted(index, index);
	}

	public boolean isCellEditable(int row, int column) {
		if (column == 0) {
			table.setCellSelectionEnabled(false);
			table.setRowSelectionAllowed(true);
			return false;
		}

		table.setRowSelectionAllowed(false);
		table.setCellSelectionEnabled(true);
		return true;
	}

	public void insertNewRowBelow() {
		// System.out.println(table.getSelectedRow());
		// if (table.getRowSelectionAllowed()
		// && table.getColumnSelectionAllowed() == false) {
		if (table.getSelectedRow() != -1) {
			table.editCellAt(table.getSelectedRow(), 0);
			Vector v = new Vector();
			v.add("");
			v.add("");
			v.add("int");
			int r = table.getSelectedRow();
			tbData.add(r + 1, v);
			fireTableRowsInserted(0, 0);
		}
		// }
	}

	public void insertNewRowAbove() {
		// System.out.println(table.getSelectedRow());
		// if (table.getRowSelectionAllowed()
		// && table.getColumnSelectionAllowed() == false) {
		if (table.getSelectedRow() != -1) {
			table.editCellAt(table.getSelectedRow(), 0);
			Vector v = new Vector();
			v.add("");
			v.add("");
			v.add("int");
			tbData.add(table.getSelectedRow(), v);
			fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
		}
		// }
	}

	public void deleteSelectedRow() {
		// System.out.println(table.getSelectedRow());
		// if (table.getRowSelectionAllowed()
		// && table.getColumnSelectionAllowed() == false) {
		int r = table.getSelectedRow();
		table.editCellAt(r, 0);
		if (table.getRowCount() > 1 && r != -1) {
			if (r != 0) {
				tbData.removeElementAt(r);
				fireTableRowsDeleted(0, 0);
			} else if (r == 0) {
				tbData.removeElementAt(r);
				fireTableRowsDeleted(1, 1);
			}
		}
		// }
	}

	public boolean checkValueAt(int[] row, int column) {
		for (int i = 0; i < row.length; i++) {
			Vector rowData = (Vector) (tbData.elementAt(row[i]));
			String value = (String) rowData.elementAt(column);
			if (value.compareToIgnoreCase("") == 0) {
				return false;
			}
		}
		return true;
	}

	public boolean checkSameColumnName() {
		table.editCellAt(table.getSelectedRow(), 0);
		for (int i = 0; i < getRowCount(); i++) {
			Vector row1 = (Vector) (tbData.elementAt(i));
			String value1 = (String) row1.elementAt(1);
			for (int j = i + 1; j < getRowCount(); j++) {
				Vector row2 = (Vector) (tbData.elementAt(j));
				String value2 = (String) row2.elementAt(1);
				if (value1.compareToIgnoreCase(value2) == 0) {
					return true;
				}
			}
		}
		return false;
	}

	public boolean checkAllColumnHasName() {
		table.editCellAt(table.getSelectedRow(), 0);
		for (int i = 0; i < getRowCount(); i++) {
			Vector rowData = (Vector) (tbData.elementAt(i));
			String value = (String) rowData.elementAt(1);
			if (value.compareToIgnoreCase("") == 0) {
				return false;
			}
		}
		return true;
	}

	public int getColumnHasName(String name) {
		table.editCellAt(table.getSelectedRow(), 0);
		for (int i = 0; i < getRowCount(); i++) {
			Vector rowData = (Vector) (tbData.elementAt(i));
			String value = (String) rowData.elementAt(1);
			if (value.compareToIgnoreCase(name) == 0) {
				return i;
			}
		}
		return -1;
	}
}
